package com.tengju.support.infrastructure.persistence.mybatis.mapper.tengju.custom;

import com.tengju.support.domain.model.resource.Position;
import com.tengju.support.domain.model.resource.ResourceId;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.AppBannerInfoDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.ArticleDO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface AppBannerInfoMapper {

    String TEMPLATE_RESULT_MAP = "app_banner_info";

    @Select("select * from app_banner_info where begin_time <= now() and end_time >= now() and  deleted = 0 order by priority desc")
    @Results(id = TEMPLATE_RESULT_MAP,
            value = {
                    @Result(column = "banner_id", property = "bannerId"),
                    @Result(column = "jump_action", property = "jumpAction"),
                    @Result(column = "img_url", property = "imgUrl"),
                    @Result(column = "title", property = "title"),
                    @Result(column = "jump_url", property = "jumpUrl"),
                    @Result(column = "create_by", property = "createBy"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime"),
                    @Result(column = "deleted", property = "deleted"),
                    @Result(column = "begin_time", property = "beginTime"),
                    @Result(column = "end_time", property = "endTime"),
                    @Result(column = "user_orientation", property = "userOrientation"),
                    @Result(column = "priority", property = "priority"),
            })
    List<AppBannerInfoDO> findAll();

    @Insert({
            "insert into app_banner_info (jump_action, img_url, title, jump_url, create_by, create_time, update_time, deleted, begin_time, end_time, user_orientation, priority,position,update_by)",
            "values (#{jumpAction}, #{imgUrl}, #{title}, #{jumpUrl}, #{createBy}, #{createTime}, #{updateTime}, #{deleted}, #{beginTime}, #{endTime}, #{userOrientation}, #{priority}, #{position}, #{updateBy})"
    })
    int insert(AppBannerInfoDO appBannerInfoDO);


    @Select("select * from app_banner_info where banner_id = #{bannerId} AND deleted = 0")
    @ResultMap(value = TEMPLATE_RESULT_MAP)
    AppBannerInfoDO findById(@Param("bannerId") Long bannerId);

    @Update({
            "update app_banner_info set title = #{title}, position = #{position}, img_url = #{imgUrl},jump_url = #{jumpUrl}" +
                    ",begin_time = #{beginTime},end_time = #{endTime},user_orientation = #{userOrientation},priority = #{priority},update_by = #{updateBy}" +
                    " where banner_id = #{bannerId}",
    })
    int update(AppBannerInfoDO appBannerInfoDO);

    @Delete("delete from app_banner_info where banner_id = #{bannerId}")
    int delete(@Param("bannerId") Long bannerId);

    @Select("<script>"
            + "     SELECT count(*) FROM app_banner_info"
            + "    where 1=1"
            + "    <if test=\"resourceId!=null and resourceId >0\">"
            + "      and banner_id = #{resourceId,jdbcType=BIGINT}"
            + "    </if>"
            + "    <if test=\"title!=null and title !=''\">"
            + "      and title = #{title,jdbcType=VARCHAR}"
            + "    </if>"
            + "    <when test=\"positions!=null and positions.size > 0\">"
            + "      and position in "
            + "      <foreach collection='positions' index='index' item='item' separator=',' open='(' close=')'>#{item.val} "
            + "      </foreach> "
            + "   </when> "
            + "</script>")
    int count(@Param("resourceId") Long resourceId,@Param("title") String title,@Param("positions") List<Position> positions);


    @Select("<script>"
            + "     SELECT * FROM app_banner_info"
            + "    where 1=1"
            + "    <if test=\"resourceId!=null and resourceId >0\">"
            + "      and banner_id = #{resourceId,jdbcType=BIGINT}"
            + "    </if>"
            + "    <if test=\"title!=null and title !=''\">"
            + "      and title = #{title,jdbcType=VARCHAR}"
            + "    </if>"
            + "    <when test=\"positions!=null and positions.size > 0\">"
            + "      and position in "
            + "      <foreach collection='positions' index='index' item='item' separator=',' open='(' close=')'>#{item.val} "
            + "      </foreach> "
            + "   </when> "
            + "    ORDER BY create_time DESC"
            + "    LIMIT #{offset},#{pageSize}"
            + "</script>")
    List<AppBannerInfoDO> queryByPage(@Param("resourceId") Long resourceId,@Param("title") String title,@Param("positions") List<Position> positions, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Update("update app_banner_info set deleted = 1 where banner_id = #{bannerId}")
    int softDelete(@Param("bannerId") Long bannerId);
}
